<template>
  <div class="internal">
    <return-internal :title="$route.params.item"></return-internal>
    <div class="context" v-if="show">
      <div class="card" v-for="item in datas" :key="item._id" v-show="item.state">
        <van-swipe-cell>
          <div class="cardcon">
            <div class="cardtop">
              <div class="name">
                {{ item.name }} &nbsp;({{
                  item.relate == "夫妻" ? "妻子" : item.relate
                }})
              </div>
              <div class="idcard">
                身份证号 &nbsp;&nbsp;&nbsp;&nbsp;{{
                  item.idcard.replace(/(.{4}).*(.{4})/, "$1********$2")
                }}
              </div>
              <div class="illcard">
                就诊卡号 &nbsp;&nbsp;&nbsp;&nbsp;{{ item.bdcard }}
              </div>
            </div>
            <div class="cradlow">
              <span class="cardcate">
                {{ item.relate == "本人" ? "默认卡" : "关系卡" }}
              </span>
            </div>
          </div>
          <template #right>
            <van-button square text="解绑" type="danger" class="delete-button" @click="cancelbindAciton(item._id)" />
          </template>
        </van-swipe-cell>
      </div>
      <div class="add" @click="gotocardAction" v-if="show">
        <span class="adds">➕</span>
        <span class="addill">{{ title }}</span>
      </div>
    </div>
    <div class="context" v-if="show2">
      <div class="card2" v-for="item in datas2" :key="item.id" @click="delbankAction(item._id)">
        <img :src="getimg(item.bankname)" alt="" class="img" />
        <div class="bankname">{{ item.bankname }}</div>
        <div class="bankid">{{ getidAction(item.bankid) }}</div>
      </div>
      <div class="add" @click="gotocardAction" v-if="show2">
        <span class="adds">➕</span>
        <span class="addill">{{ title }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import img1 from "../../../assets/images/img1.jpg";
import img2 from "../../../assets/images/img2.png";
import img3 from "../../../assets/images/img3.jpg";
import img4 from "../../../assets/images/img4.jpg";
import img5 from "../../../assets/images/img5.jpg";
import { SwipeCell, Button, Card, Dialog, Toast } from "vant";
import { cancelbindCardApi } from "../../../api/patient";
export default {
  components: {
    [SwipeCell.name]: SwipeCell,
    [Button.name]: Button,
    [Card.name]: Card,
    [Toast.name]: Toast,
    [Dialog.Component.name]: Dialog.Component,
  },
  data() {
    return {
      datas: [],
      title: "",
      datas2: [],
      show: "",
      show2: "",
    };
  },
  methods: {
    async getcard() {
      let res = await axios.request({
        method: "post",
        url: "/api/patient/getcardinfo",
        headers: {
          Authorization: "Bearer " + localStorage.getItem("token"),
        },
      });
      let res1 = await axios.request({
        method: "post",
        url: "/api/bank/inquirebank",
        headers: {
          Authorization: "Bearer " + localStorage.getItem("token"),
        },
      });
      if (this.$route.params.item == "电子健康卡") {
        this.show = true;
        this.show2 = false;
        this.datas2 = [];
        this.datas = res.data.data;
        this.title = "添加就诊人";
      } else {
        this.show = false;
        this.show2 = true;
        this.datas = [];
        this.datas2 = res1.data.data;
        this.title = "添加银行卡";
      }
    },
    gotocardAction() {
      if (this.$route.params.item == "电子健康卡") {
        this.$router.push({ name: "FilingInstructions" });
      } else {
        this.$router.push({
          name: "addbankcard",
          params: { item: "添加银行卡" },
        });
      }
    },
    getimg(name) {
      if (name == "中国农业银行") {
        return img1;
      } else if (name == "中国招商银行") {
        return img2;
      } else if (name == "中国建设银行") {
        return img3;
      } else if (name == "中国工商银行") {
        return img4;
      } else {
        return img5;
      }
    },
    delbankAction(id) {
      this.$router.push({
        name: "delbankcard",
        params: { item: "移除银行卡", id },
      });
    },
    getidAction(id) {
      id = id.replace(/^.{16}/, "*".repeat(16));
      let carry = 0;
      let str = "";
      for (let i = 0; i < id.length; i++) {
        carry++;
        str += id[i];
        if (carry == 4) {
          str += "\xa0\xa0\xa0\xa0 ";
          carry = 0;
        }
      }
      return str;
    },
    cancelbindAciton(id) {
      Dialog.confirm({
        title: "解绑电子健康卡",
        message: "是否确定解除绑定此电子健康卡",
      })
        .then(() => {
          cancelbindCardApi({ id });
          Toast("解绑成功");
          location.reload();
        })
        .catch(() => {
          console.log("取消");
        });
    },
  },
  computed: {
    idcard() { },
  },
  mounted() {
    this.getcard();
  },
};
</script>

<style scoped lang="scss">
@function vw($px) {
  @return ($px/375) * 100vw;
}

.context {
  position: relative;
  top: 44px;
  width: 100%;
  height: calc(100% - 44px - vw(10));
  overflow: auto;

  .card {
    width: vw(342);
    height: vw(201);
    display: flex;
    flex-direction: column;
    margin: auto;
    border-radius: vw(5);
    overflow: hidden;
    margin-top: vw(10);

    .van-swipe-cell {
      height: 100%;

      ::v-deep .van-swipe-cell__wrapper {
        height: 100%;

        .cardcon {
          height: 100%;
        }
      }
    }

    .delete-button {
      height: 100%;
    }

    .cardtop {
      width: 100%;
      height: 75%;
      background: rgba(46, 199, 201, 1);
      font-weight: 400;
      font-style: normal;
      color: #333;
      font-size: vw(16);
      position: relative;

      .name {
        position: absolute;
        left: vw(18);
        top: vw(20);
      }

      .idcard {
        position: absolute;
        left: vw(18);
        top: vw(80);
      }

      .illcard {
        position: absolute;
        left: vw(18);
        bottom: vw(20);
      }
    }

    .cradlow {
      width: 100%;
      height: 25%;
      background: #fff;
      flex: 1;
      font-weight: 400;
      font-style: normal;
      color: #999999;
      font-size: vw(14);
      position: relative;

      .cardcate {
        position: absolute;
        left: vw(18);
        top: vw(18);
      }
    }
  }

  .add {
    width: vw(342);
    margin: vw(20) auto;
    height: vw(82);
    background: #fff;
    border: vw(1) dashed #ccc;
    position: relative;

    .adds {
      font-size: vw(20);
      color: #999;
      position: absolute;
      left: 50%;
      transform: translateX(-70%);
      top: vw(18);
    }

    .addill {
      font-weight: 400;
      font-style: normal;
      color: #999999;
      font-size: vw(15);
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      bottom: vw(20);
    }
  }

  .card2 {
    width: vw(341);
    height: vw(151);
    display: flex;
    flex-direction: column;
    margin: auto;
    border-radius: vw(5);
    overflow: hidden;
    margin-top: vw(10);
    background-color: rgba(1, 155, 130, 1);
    position: relative;

    .img {
      position: absolute;
      left: vw(25);
      top: vw(15);
    }

    .bankname {
      position: absolute;
      font-size: vw(17);
      top: vw(60);
      color: #fff;
      top: vw(20);
      left: vw(95);
    }

    .bankid {
      position: absolute;
      font-size: vw(16);
      color: #fff;
      left: vw(25);
      top: vw(110);
    }

    img {
      width: vw(50);
      height: vw(47);
      border-radius: 50%;
    }
  }
}
</style>
